package cs2114.antiFingerMines;

import android.app.Instrumentation.ActivityMonitor;
import android.view.KeyEvent;

// -------------------------------------------------------------------------
/**
 *  Tests the AntiFingerMinesHard mode
 *
 *  @author Blake Whaley
 *  @author Graham McLaughlin (gjm1324)
 *  @author William Vuong (wnvuong)
 *  @version 2012.29.4
 */
public class AntiFingerMinesHardTest
    extends student.AndroidTestCase<AntiFingerMinesHard>
{
    private MineField           model;
    private AntiFingerMinesView view;
    private float               mineOld;
    private ActivityMonitor     difficultyMonitor;

    /**
     * Create a new AntiFingerMinesHardTest object.
     */
    public AntiFingerMinesHardTest()
    {
        super(AntiFingerMinesHard.class);
    }

    /**
     * Sets up the variables needed for testing
     */
    public void setUp()
        throws Exception
    {
        view = getView(AntiFingerMinesView.class, R.id.mineView);
        model = getActivity().getModel();
        mineOld = model.getMine(1).getX();

        difficultyMonitor =
            new ActivityMonitor(
                AntiFingerMinesHard.class.getName(),
                null,
                false);
        getInstrumentation().addMonitor(difficultyMonitor);
    }


    /**
     * Tests functionality of quit button in the menu
     */
    public void testMenuQuit()
    {

        AntiFingerMinesHard activity = getActivity();
        ActivityMonitor am =
            getInstrumentation().addMonitor(
                AntiFingerMinesEasy.class.getName(),
                null,
                false);

        sendKeys(KeyEvent.KEYCODE_MENU);

        getInstrumentation().invokeMenuActionSync(
            activity,
            cs2114.antiFingerMines.R.id.Quit,
            0);

        assertFalse(getInstrumentation().checkMonitorHit(am, 1));

    }


    /**
     * Tests functionality of the restart button in the menu
     */
    public void testMenuRestart()
    {

        AntiFingerMinesHard activity = getActivity();
        ActivityMonitor am =
            getInstrumentation().addMonitor(
                AntiFingerMinesEasy.class.getName(),
                null,
                false);

        sendKeys(KeyEvent.KEYCODE_MENU);

        getInstrumentation().invokeMenuActionSync(
            activity,
            cs2114.antiFingerMines.R.id.Restart,
            0);

        assertFalse(getInstrumentation().checkMonitorHit(am, 1));

        Float mineNew = getActivity().getModel().getMine(1).getX();

        assertFalse(mineNew.equals(mineOld));
    }


    /**
     * Tests functionality of the difficulty button in the menu
     */
    public void testzMenuDifficulty()
    {

        AntiFingerMinesHard activity = getActivity();
        ActivityMonitor am =
            getInstrumentation().addMonitor(
                AntiFingerMinesEasy.class.getName(),
                null,
                false);

        sendKeys(KeyEvent.KEYCODE_MENU);

        getInstrumentation().invokeMenuActionSync(
            activity,
            cs2114.antiFingerMines.R.id.Difficulty,
            0);

        assertFalse(getInstrumentation().checkMonitorHit(am, 1));

        activity = getActivity();
        activity.finish();
    }


    /**
     * Tests the activity checking to see if we lost the game or not
     */
    public void testOnTouchLoseandRestart()
    {
        // Drags the finger straight to a mines location
        model.setFingerLocation(model.getMine(1).getX(), model.getMine(1).getY());
        drag(view, 0,0,0,0);
        assertTrue(model.loseStatus);
        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        assertEquals("Traverse the Minefield!", getActivity().getStatusText());
    }

    /**
     * Tests the activity checking to see if we lost the game or not
     */
    public void testOnTouchLoseandQuit()
    {
        // Drags the finger straight to a mines location
        model.setFingerLocation(model.getMine(1).getX(), model.getMine(1).getY());
        drag(view, 0,0,0,0);
        assertTrue(model.loseStatus);
        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
    }

    /**
     * Tests the activity checking to see if we won the game or not
     */
    public void testOnTouchWin()
    {
        // By making the step, it can skip over all the mines

        model.setFingerLocation(model.getWidth()/2, model.endOfField + 10);
        drag(view, 0,0,0,0);
        assertTrue(model.winStatus);
    }

    /**
     * Tests the activity checking to see if we won the game or not
     */
    public void testOnTouchWinandRestart()
    {
        // By making the step, it can skip over all the mines

        model.setFingerLocation(model.getWidth()/2, model.endOfField + 10);
        drag(view, 0,0,0,0);
        assertTrue(model.winStatus);
        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        assertEquals("Traverse the Minefield!", getActivity().getStatusText());
    }

    /**
     * Tests the activity checking to see if we won the game or not
     */
    public void testOnTouchWinandQuit()
    {
        // By making the step, it can skip over all the mines

        model.setFingerLocation(model.getWidth()/2, model.endOfField + 10);
        drag(view, 0,0,0,0);
        assertTrue(model.winStatus);
        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
    }

}
